---
title: "Findex 2025 Replication"
output: html_document
---
```{r}
# NOTE: please add your own file paths wherever the text "INSERT" is
```


# ```{r setup, include=FALSE}
# knitr::opts_chunk$set(echo = TRUE)
# 
# renv::init()
# 
# # Install required libraries
# packages <- c("haven", "dplyr", "data.table", "readxl", "lubridate", "readr", "tidyverse", "labelled", "arrow", "scales",
#               # for figures
#               "ggplot2", "showtext", "ggtext")
# 
# 
# # Function to install (if needed) and load packages
# install_and_load <- function(pkg_list) {
#   for (pkg in pkg_list) {
#     if (!require(pkg, character.only = TRUE)) {
#       install.packages(pkg)
#       library(pkg, character.only = TRUE)
#     } else {
#       library(pkg, character.only = TRUE)
#     }
#   }
# }
# 
# # Call the function
# install_and_load(packages)
# 
# 
# # for figures
# font_add_google("Nunito Sans")
# showtext_auto()
# 
#  renv::snapshot()

```


```{r}

# Clear workspace
rm(list = ls())

# set file path to source the data from

# Define your folders once
folder_data      <- "C:/WBG/github/361/OneDrive_2025-07-02/For DIME/Data/Raw/"
folder_code      <- "C:/WBG/github/361/OneDrive_2025-07-02/For DIME/Code/Cleaning/"

# this is where the data that we output will go
folder_data_output <- "C:/WBG/github/361/OneDrive_2025-07-02/For DIME/Data/Cleaned/"

#renv::restore(lockfile = "renv.lock", prompt = FALSE)

```


Cleaning the data and outputting the database
```{r}
# Step 1: run the initial data cleaning code - this will add regional weights, findex identifiers, and also add in other country-level data we care about, specifically population from WDI - population is lagged for all years

# source(file.path(folder_code, "Prereg_Masterdata_2024.R"))
# 
# source(file.path(folder_code, "Exclusions_allyears.R"))
# 
# # Identifiers file creates indicators for the 2024 data showing which questionnaire people are being asked. For example, developing countries aren't asked 
# 
# 
# # rename data
# dataframe_2024 <- gallup_data
# 
# # Remove all objects except 'dataframe_2024' - this makes things faster and prevents a crash
# all_objects <- ls()
# rm(list = setdiff(all_objects, c("dataframe_2024", "folder_data", "folder_code", "folder_data_output")))
# 
# # run the identifier file
# source(file.path(folder_code, "Identifiers.R"))
# 
# # run the AD_14_17_21_22.R file
# source(file.path(folder_code, "AD_14_17_21_22.R"))
# 
# 
# # non-findex year data
# source(file.path(folder_code, "Interyear_AD.R"))
# 
# 
# # List all objects in the environment
# all_objects <- ls()
# # Remove all objects except 'my_data'
# rm(list = setdiff(all_objects, c("dataframe_2024", "folder_data", "folder_code", "folder_data_output")))
# 
# # run the AD file
# source(file.path(folder_code, "AD_2024.R"))
# gc()
# 
# 
# # run TS_Summary - this is the file that takes in all of the data and creates country-year level averages
# 
# source(file.path(folder_code, "TS_Summary.R"))

```

Creating all report figures
```{r}

data <- readr::read_csv(file.path(folder_data_output, "TS_for_Figures_24_REGIONS.csv"))


# Vector of chapter names
#chapter_names <- c("Accounts (2.1)", "Connectivity (1)", "Savings (3.1)", "Borrowing (3.2)", "Financial Health (4)", "Payments (3.3)", "Account Use (SP3)", "Executive Summary", "ID (SP1)", "Maps", "Natural Disasters (SP2)") 

chapter_names <- c("Natural Disasters (SP2)") 


# Base path (edit this part)
base_path <- "C:/WBG/github/361/OneDrive_2025-07-02/For DIME/Code/Figures"

# Loop over each chapter
for (chapter in chapter_names) {
  
  # Construct the script folder path for this chapter
  script_folder <- file.path(base_path, chapter)

  # Where to save figures 
  folder_path <- file.path("C:/WBG/github/361/OneDrive_2025-07-02/For DIME/Outputs/Generated Figures", chapter)

  # List all R script files in the folder
  script_files <- list.files(path = script_folder, pattern = "\\.R$", full.names = TRUE)

  # Source each script while suppressing warnings and skipping empty files
  lapply(script_files, function(file) {
    if (file.info(file)$size > 0) {  # Check if the file is not empty
      tryCatch({
        suppressWarnings(source(file))
        message(paste("Successfully ran:", file))
      }, error = function(e) {
        message(paste("Error in file:", file, ":", e$message))
      })
    } else {
      message(paste("Skipped empty file:", file))
    }
  })
}


```


